import request from "@/utils/request";
import MyStorage from "@/utils/storage";
import storage from "@/utils/storage";

interface ILogin {
    doLogin : (type:number,phone:string,password:string) => Promise<boolean>
    isLogin : () => boolean
    getToken : () => string
    upExTime : () => void
    setFriendChain : (friend_chain:string) => void
    isOverdue :() => boolean
    signOut :() => void
}

class Login implements ILogin{

    static instance : Login;

     PHONE = 0;
     Email  = 1;
     UserName  = 2;

    /**
     * 去登录
     * @param type
     * @param info
     * @param password
     */
    public async doLogin(type:number,info:string,password:string) :Promise<boolean>
    {
        let blog = false;
        let params = {};
        if (this.isLogin()) {
            blog = true;
        } else {
            if (type == this.PHONE) {
                params = {
                    "phone":info,
                    "password":password
                }
            } else if (type == this.Email) {
                params = {
                    "email":info,
                    "password":password
                }
            } else {
                params = {
                    "username":info,
                    "password":password
                }
            }
            await request.post("login", params).then((res: any) => {
                if (res.code == 200) {
                    this.setToken(res.data)
                    blog = true;
                }
            })
        }
        return new Promise((resolve) => {
            resolve(blog)
        })
    }

    /**
     * 判断是否登陆
     */
    public isLogin():boolean
    {
        return (this.getToken() != null && !this.isOverdue());
    }

    /**
     * 获取token
     */
    public getToken() :string
    {
        return MyStorage.get("token")
    }

    /**
     * 设置token
     * @param data
     * @protected
     */
    protected setToken(data) : void{
        MyStorage.set<string>("token",data.token)
        this.setUserName(data.username)
        this.setEmail(data.email)
        this.setUserIcon(data.usericon)
        this.setFriendChain(data.friend_chain)
        this.upExTime()
    }

    /**
     * 获取用户名
     */
    public getUserName() :string
    {
        return MyStorage.get("username")
    }

    /**
     * 缓存用户名
     *
     * @param username
     */
    public setUserName(username:string):void{
        MyStorage.set<string>("username",username)
    }

    /**
     * 获取用户头像
     */
    public getUserIcon() :string
    {
        return MyStorage.get("usericon")
    }

    /**
     * 缓存用户头像
     * @param usericon
     */
    public setUserIcon(usericon:string):void{
        MyStorage.set<string>("usericon",usericon)
    }

    /**
     * 获取用户邮箱
     */
    public getEmail() :string
    {
        return MyStorage.get("email")
    }

    /**
     * 缓存用户邮箱
     * @param email
     */
    public setEmail(email:string):void{
        MyStorage.set<string>("email",email)
    }

    /**
     * 获取用户友链
     */
    public getFriendChain() :string
    {
        return MyStorage.get("friend_chain")
    }

    /**
     * 缓存用户友链
     * @param friend_chain
     */
    public setFriendChain(friend_chain:string):void{
        MyStorage.set<string>("friend_chain",friend_chain)
    }

    /**
     * 更新过期时间
     */
    public upExTime():void
    {
        this.setExTime(this.getTwoHoursLater())
    }

    /**
     * 设置过期时间
     * @param exTime
     * @protected
     */
    protected setExTime(exTime:number){
        MyStorage.set<number>("ExpirationTimeInt",exTime)
    }

    /**
     * 获取过期时间
     * @protected
     */
    protected getExTime() : number{
        const exTime = MyStorage.get("ExpirationTimeInt")
        if(exTime!= null) return exTime
        return 0
    }

    /**
     * 获取两小时后时间戳
     * @protected
     */
    protected getTwoHoursLater() :number {
        return( (new Date()).valueOf() + 2*60*60*1000)
    }

    /**
     * 判断登陆时长是否过期
     */
    public isOverdue() :boolean
    {
        const timestamp = (new Date()).valueOf()
        console.log(timestamp,"timestamp")
        return timestamp > this.getExTime()
    }

    /**
     * 退出登陆
     */
    public signOut():void{
        storage.remove('token')
        storage.remove('username')
        storage.remove('email')
        storage.remove('usericon')
        storage.remove('friend_chain')
        this.setExTime(0)
    }

    static getInstance(){
        if (!Login.instance) {
            Login.instance = new Login();
        }
        return Login.instance;
    }
}


const instance = Login.getInstance();

export default instance;